# Boyoon Lee "The Impact of Educational Content on Anti-Immigrant Attitudes"
# 6. Online Appendix F (Table F.8, Table F.9, Table F.10, Table F.11)
# Last updated: 2022-11-16


# Initial settings --------------------------------------------------------

# Set directory
setwd("Your Path Here")

# Packages
library(ggplot2) 
library(miceadds) # for lm.cluster
library(ClusterBootstrap) # for Cluster Bootstrap SE
library(tidyr)
library(dplyr)
library(descr)
library(stargazer)


# Load data  --------------------------------------------------------------
data <- read.csv("./cleaned_TSCS_did.csv", header=TRUE, sep=",")


# Recode / Re-class  ------------------------------------------------------

### Make date variable using Birth year and Birth month
# Since there is no date of birth available, I set the date to the first day of the month
data$date <- as.Date(with(data, paste(birth_yr, birth_month, "01", sep="-")), "%Y-%m-%d")

### Only leave school cohorts before 2002 
# In 2001, Knowing Taiwan series discontinued and replaced with the New Grade 1-9 curriculum (9 year curriculum)
# In addition, from 1968, junior vocational school shut down and expanded senior vocational school
data<-subset(data, data$school_yr<2002&data$school_yr>1967)


# Subsets for the Comparison Group ---------------------------------------

# Main comparison group 
yr13_22<-subset(data,school_yr>1987 & school_yr<2002)
yr13_22<-subset(yr13_22,edu_level>3) # those who graduated from at least junior high schools

# Alternative comparison group
yr13_18<-subset(data,school_yr>1991 & school_yr<2002) # to high school (age 17-18)
yr13_26<-subset(data,school_yr>1983 & school_yr<2002) # to 25-26 (4 years older to match the time window to 17-18)



#############################################################
##           [APPENDIX F] Robustness Checks                ##
#############################################################


# Table F.8  -----------------------------------------------------------

### (1) Aged from 13-14 to 17-18
did.1318.c1 <- lm.cluster(data= yr13_18, 
                          formula= num_imm ~ post + group + TG
                          + female + married + employed + social_class+ edu_uni
                          + post*prts_gen 
                          + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                          cluster = as.integer(yr13_18$school_yr)*as.integer(yr13_18$group_plus))
summary(did.1318.c1)

### (2) Aged from 13-14 to 25-26
did.1326.c1 <- lm.cluster(data= yr13_26, 
                          formula= num_imm ~ post + group + TG
                          + female + married + employed + social_class+ edu_uni
                          + post*prts_gen
                          + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                          cluster = as.integer(yr13_26$school_yr)*as.integer(yr13_26$group_plus))
summary(did.1326.c1)


# Table F.9 ------------------------------------------------------------

### (1) Short-term sample
young<-subset(yr13_22,yr13_22$year<2011)
young.cl <- lm.cluster(data= young, 
                        formula= num_imm ~ post + group + TG 
                        + female + married + employed + social_class+ edu_uni
                        + post*prts_gen 
                        + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                        cluster = as.integer(young$school_yr)*as.integer(young$group_plus))
summary(young.cl)

### (2) Long-term Sample
old<-subset(yr13_22,yr13_22$year>2011)
old.cl <- lm.cluster(data= old, 
                       formula= num_imm ~ post + group + TG 
                       + female + married + employed + social_class+ edu_uni
                       + post*prts_gen 
                       + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                       cluster = as.integer(old$school_yr)*as.integer(old$group_plus))
summary(old.cl)


# Table F.10 ------------------------------------------------------------

### (1) Bootstrapped CRSEs
clus1<-as.integer(yr13_22$school_yr)*as.integer(yr13_22$group_plus)
# 1000 bootstrap sample
set.seed(200403)
did.bcl.c1 <- clusbootglm(num_imm ~ post + group + TG 
                          + female + married + employed + social_class+ edu_uni
                          + post*prts_gen 
                          + as.factor(edu_level)  + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year),
                          data=yr13_22, clusterid = clus1, family = gaussian, 
                          B=1000, confint.level = 0.95)
summary(did.bcl.c1)
# 5000 bootstrap sample
set.seed(200403)
did.bcl2.c1 <- clusbootglm(num_imm ~ post + group + TG 
                           + female + married + employed + social_class+ edu_uni
                           + post*prts_gen 
                           + as.factor(edu_level)  + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year),
                           data=yr13_22, clusterid = clus1, family = gaussian, 
                           B=5000, confint.level = 0.95)
summary(did.bcl2.c1)

### (2) Education-level CRSEs
clus2<-as.integer(yr13_22$school_yr)*as.integer(yr13_22$edu_level)
did.cl2.c1 <- lm.cluster(data= yr13_22, 
                         formula= num_imm ~ post + group + TG
                         + female + married + employed + social_class+ edu_uni
                         + post*prts_gen 
                         + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                         cluster = clus2)
summary(did.cl2.c1)


# Table F.11 ------------------------------------------------------------

### (1) Happiness
# Taking all things together, how happy would you say things are recently?
# Lower values represent more happy
happy.cl <- lm.cluster(data= yr13_22, 
                       formula= happy ~ post + group + TG
                       + female + married + employed + social_class 
                       + post*prts_gen 
                       + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                       cluster = clus1)
summary(happy.cl)

### (2) Success
# Low number strongly agree to "As long as one can bear hardship, he/she will surely succeed"
att.success <- lm.cluster(data= yr13_22, 
                          formula= att_succeed ~ post + group + TG
                          + female + married + employed + social_class 
                          + post*prts_gen 
                          + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                          cluster = clus1)
summary(att.success)

### (3) Inequality
# Low number strongly agree to "Nowadays the rich are getting richer.People like me cannot catch up with them no matter how hard we try"
att.rich <- lm.cluster(data= yr13_22, 
                       formula= att_rich ~ post + group + TG
                       + female + married + employed + social_class 
                       + post*prts_gen 
                       + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                       cluster = clus1)
summary(att.rich)
